package com.wiscamp.ninechapters.tags.infrastructure.mappers;

import com.wiscamp.ninechapters.tags.infrastructure.po.EntityTagPO;
import com.wiscamp.ninechapters.tags.infrastructure.po.TagView;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Collection;

/**
 * 实体标签关联仓储对象
 *
 * @author yifeng
 */
@Repository
public interface EntityTagMapper extends JpaRepository<EntityTagPO, Long>, JpaSpecificationExecutor<EntityTagPO> {
    @Query(value = "select e from EntityTagPO e where e.entityId = :entityId and e.primaryKeyId = :primaryKeyId and e.tagId = :tagId")
    EntityTagPO findEntityTag(long entityId, long primaryKeyId, long tagId);

    @Query(value = "select e.primaryKeyId from EntityTagPO e where e.entityId = :entityId and e.tagId in (:tagIds)")
    Collection<Long> findPrimaryIdsByTags(long entityId, Collection<Long> tagIds);

    @Query(value = "select e.tagId from EntityTagPO e where e.entityId = :entityId and e.primaryKeyId = :primaryKeyId")
    Collection<Long> findTagIdsByPrimaryKey(long entityId, long primaryKeyId);

    @Query(value = "select e from TagView e, EntityTagPO et where e.tagId = et.tagId and et.entityId = :entityId and et.primaryKeyId = :primaryKeyId and e.enabled = true")
    Collection<TagView> findTagsByPrimaryKey(long entityId, long primaryKeyId);
}